<?php

class Admin_UsersModel extends BlogAdminBaseModel
{
    /**
     * Check that given user has given password.
     *
     * @param string $username
     * @param string $password
     * @return boolean
     */
    public function checkUserPassword($username, $password)
    {
        $query = Doctrine_Query::create()
            ->from('User u')
            ->where('u.login_name = ? and u.user_type = ?', array($username, 'plain'))
            ;
        $result = $query->execute();
        $user = $result->getFirst();

        if (!$user instanceof User) {
            return FALSE;
        }
        
        $orig_hash = $user->password_hash;
        $salt = substr($orig_hash, 0, 4);
        $hash = BlogUser::computeSaltedHash($password, $salt);
        
        if ($orig_hash != $salt.$hash) {
            return FALSE;
        }
        
        return TRUE;
    }
    
    /**
     * Retreive User object instance by it's db id.
     *
     * @param int $user_id
     * @return User false if there are no such user
     */
    public function getUser($user_id)
    {
        return Doctrine::getTable('User')->find($user_id);
    }
    
    /**
     * Get list of all users
     * 
     * @return Collection list of User objects
     */
    public function findUsers()
    {
        $users = Doctrine::getTable('User')->findAll();
        return $users;
    }
    
    /**
     * Update User data.
     *
     * @param User $user
     * @param string $display_name
     * @param string $email
     * @param string $password
     * @param boolean $trusted
     */
    public function updateUserData($user, $display_name, $email, $website, $password, $trusted)
    {
        $con = $this->getContext()->getDatabaseManager()->getDatabase('blog')->getConnection();
        $con->beginTransaction();
        $login_name = $user->login_name;
        
        try {
            $modified = false;
            if (!is_null($display_name)) {
                $user->display_name = $display_name;
                $modified = true;
            }
            if (!is_null($email)) {
                $user->email = $email;
                $modified = true;
            }
            if (!is_null($website)) {
                $user->website = $website;
                $modified = true;
            }
            if (!is_null($password)) {
                $user->password_hash = BlogUser::genPasswordHash($password);
                $modified = true;
            }
            if (!is_null($trusted)) {
                // find 'trusted-user' role
                $trusted_user_role = null;

                /* @var $trusted_user_role UserRole */
                foreach ($user->Roles as $role) {
                    if ($role->role_name === 'trusted-user') {
                        $trusted_user_role = $role;
                        break;
                    }
                }
                if ($trusted === TRUE) {
                    // grant role 'trusted-user'
                    if (!$trusted_user_role instanceof UserRole) {
                        $trusted_user_role = new UserRole();
                        $trusted_user_role->role_name = 'trusted-user';
                        $user->Roles->add($trusted_user_role);
                        $modified = true;
                    }
                } else {
                    // revoke role 'trusted-user'
                    if ($trusted_user_role instanceof UserRole) {
                        $trusted_user_role->delete($con);
                        $modified = true;
                    }
                }
            }
            if ($modified) {
                $user->save($con);
                $con->commit();
            }
            
        } catch (Doctrine_Exception $e) {
            $con->rollback();
            throw $e;
        }
        
    }
}

?>
